<?php
namespace Views;

require_once('\Models\Database.php');
require_once('\Models\Album.php');
require_once('\Models\ImageCatalog.php');
require_once('\Models\Image.php');
require_once('\Views\PageView.php');

class ViewAlbumsView
{
	private $_pv;
	
	public function __construct()
	{
		$this->_pv = new \Views\PageView();
	}
	
	public function viewAlbums(\Models\AlbumArray $albums)
	{
		$html = "";
		
		foreach ($albums->get() as $album)
			$html .= "<p><a href='gallery.php?".$this->_pv->albumId."=".$album->AlbumId()."'>".$album->Title()." [".$album->Year()."]</a></p>";
		
		return $html;
	}
	
	public function getAlbumImages(\Models\ImageArray $images)
	{
		$html = "<div id='gallery'><ul>";
		
		foreach ($images->get() as $image)
			$html .= "<li><a href='".$image->ImageUrl()."'><img src='".$image->ThumbUrl()."' alt='".$image->Description()."' title='".$image->Description()."' /></a></li>";
		
		$html .= "</ul></div>";
		
		return $html;
	}
	
	public function getAlbumInfo(\Models\Album $album)
	{
		$html = "<p><strong>Beskrivning:</strong> " . $album->Description() . "</p>";
		$html .= "<p><strong>År:</strong> " . $album->Year() . "</p>";
		
		return $html;
	}
	
	public function albumCategoriesForm(\Models\UserArray $users, $selectedUserId = "", $selectedYear = "")
	{
		$html =
		"<form method='get'>
			<label for='".$this->_pv->albumYear."'>År</label>
				<select id='".$this->_pv->albumYear."' name='".$this->_pv->albumYear."'>
					<option value='0'>Alla år</option>";
		
		$year = date("Y");
		
		// De första albumet startade år 1963
		for ($i=$year; $i>=1963; $i--)
		{
			if($i == $selectedYear)
				$html .= "<option value='$i' selected='selected'>$i</option>";
			else
				$html .= "<option value='$i'>$i</option>";
		}
			
		$html .= "
			</select>
			<label for='".$this->_pv->userId."'>Användare</label>
			<select id='".$this->_pv->userId."' name='".$this->_pv->userId."'>
				<option value='0'>Alla användare</option>";
		
		foreach ($users->get() as $user)
		{
			if($user->UserId() == $selectedUserId)
				$html .= "<option selected='selected' value='".$user->UserId()."'>".$user->FirstName()." ".$user->LastName()."</option>";
			else
				$html .= "<option value='".$user->UserId()."'>".$user->FirstName()." ".$user->LastName()."</option>";
		}
			
		$html .= "
			</select>
			<input type='submit' value='Sök' />
		</form>
		<hr />";
		
		return $html;
	}
	
	public function getUserId()
	{
		// 0 om ingen användare är vald
		if(
		isset($_GET[$this->_pv->userId]) && 
		is_numeric($_GET[$this->_pv->userId]) && 
		$_GET[$this->_pv->userId] != 0
		)
			return $_GET[$this->_pv->userId];
		else
			return false;
	}
	
	public function getAlbumYear()
	{
		// 0 om ingen användare är vald
		if(
		isset($_GET[$this->_pv->albumYear]) && 
		is_numeric($_GET[$this->_pv->albumYear]) && 
		$_GET[$this->_pv->albumYear] != 0
		)
			return $_GET[$this->_pv->albumYear];
		else
			return false;
	}
}

?>